package com.byq.mm.smartlib.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.byq.mm.smartlib.activity.Constants;


public class SmartDbConnector extends SQLiteOpenHelper {

  private static final String TAG = "AimDbConnector";

  public SmartDbConnector(Context context) {
    super(context, Constants.DATABASE, null, Constants.DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
	  
	  
	db.execSQL("CREATE TABLE " + Constants.Device.TABLE_NAME + " ("
		        + Constants.Device.GLOBALID + " TEXT PRIMARY KEY,"
		        + Constants.Device.ID + " TEXT,"
		        + Constants.Device.TRANSPORTID + " TEXT,"
		        + Constants.Device.NAME + " TEXT,"
		        + Constants.Device.PARENT + " TEXT,"
		        + Constants.Device.TYPE + " TEXT);"
		    );  
	db.execSQL("CREATE TABLE " + Constants.ClusterEntry.TABLE_NAME + " ("
			+ Constants.ClusterEntry.GENERATED_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
	        + Constants.ClusterEntry.ID + " TEXT,"
	        + Constants.ClusterEntry.DEVICEID + " TEXT,"

	  
	        + Constants.ClusterEntry.ALIAS + " TEXT);"
	    );  
	db.execSQL("CREATE TABLE " + Constants.ServerSecurity.TABLE_NAME + " ("
			
	        + Constants.ServerSecurity.ID + " TEXT PRIMARY KEY,"
	        + Constants.ServerSecurity.DEVICEID + " TEXT,"
	        + Constants.ServerSecurity.CLUSTERID + " TEXT,"
	        + Constants.ServerSecurity.PERMITTED + " INT)"
	    );
	db.execSQL("CREATE TABLE " + Constants.ClientEntry.TABLE_NAME + " ("
			
	        + Constants.ClientEntry.ID + " TEXT PRIMARY KEY,"
	        + Constants.ClientEntry.DEVICEID + " TEXT,"
	        + Constants.ClientEntry.CLUSTERID + " TEXT,"
	        + Constants.ClientEntry.CLIENTTRANSPORT + " TEXT,"
	        + Constants.ClientEntry.PERMITTED + " INT)"
	
	    );
	
	

    db.execSQL("CREATE TABLE " + Constants.InteractionEntry.TABLE_NAME + " ("
            + Constants.InteractionEntry.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + Constants.InteractionEntry.DATE + " INT,"
            + Constants.InteractionEntry.DEVICEID + " TEXT,"
            + Constants.InteractionEntry.CLUSTERID + " TEXT,"
            + Constants.InteractionEntry.FROM + " TEXT,"
            + Constants.InteractionEntry.TO + " TEXT,"
            + Constants.InteractionEntry.DIRECTION + " TEXT,"
            + Constants.InteractionEntry.ACTION + " TEXT,"
            + Constants.InteractionEntry.OPERATION + " TEXT);"
        );
    db.execSQL("CREATE TABLE " + Constants.Conversation.TABLE_NAME + " ("
        + Constants.Conversation.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + Constants.Conversation.DATE + " INT,"
        + Constants.Conversation.CHAT + " TEXT,"
        + Constants.Conversation.FROM + " TEXT,"
        + Constants.Conversation.TO + " TEXT,"
        + Constants.Conversation.MSG + " TEXT,"
        + Constants.Conversation.NEW + " INT);"
    );
    db.execSQL("CREATE TABLE " + Constants.TABLE_LOG + " ("
        + Constants.TABLE_LOG_FIELD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + Constants.TABLE_LOG_FIELD_DATE + " INT,"
        + Constants.TABLE_LOG_FIELD_TIME + " TIME,"
        + Constants.TABLE_LOG_FIELD_FROM + " TEXT,"
        + Constants.TABLE_LOG_FIELD_RESOURCE + " TEXT,"
        + Constants.TABLE_LOG_FIELD_MSG + " TEXT);"
    );
    db.execSQL("CREATE TABLE " + Constants.TABLE_BUDDY + " ("
        + Constants.TABLE_BUDDY_FIELD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + Constants.TABLE_BUDDY_FIELD_JID + " TEXT,"
        + Constants.TABLE_BUDDY_FIELD_NAME + " TEXT,"
        + Constants.TABLE_BUDDY_FIELD_STATUS + " TEXT,"
        + Constants.TABLE_BUDDY_FIELD_PRESENCETYPE + " INTEGER,"
        + Constants.TABLE_BUDDY_FIELD_PRESENCEMODE + " INTEGERT,"
        + Constants.TABLE_BUDDY_FIELD_MSG + " TEXT);"
    );
    db.execSQL("CREATE TABLE " + Constants.TABLE_GROUP + " ("
        + Constants.TABLE_GROUP_FIELD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + Constants.TABLE_GROUP_FIELD_GROUP + " TEXT,"
        + Constants.TABLE_GROUP_FIELD_JID + " TEXT);"
    );

    db.execSQL("CREATE TABLE " + Constants.TABLE_STATUSMSG + " ("
        + Constants.TABLE_STATUSMSG_FIELD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + Constants.TABLE_STATUSMSG_FIELD_MSG + " TEXT,"
        + Constants.TABLE_STATUSMSG_FIELD_ACTIVE + " TEXT,"
        + Constants.TABLE_STATUSMSG_FIELD_LASTUSED + " INT);"
    );
    ContentValues cv = new ContentValues();
    cv.put(Constants.TABLE_STATUSMSG_FIELD_MSG, "");
    cv.put(Constants.TABLE_STATUSMSG_FIELD_ACTIVE, true);
    db.insert(Constants.TABLE_STATUSMSG, null, cv);

    Log.i(TAG, "Tables created");


  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // do nothing
    Log.w(TAG, "onUpgrade called, but nothing done.");
  }
}
